

import { Checkbox } from 'antd';
import './forms.scss';
import { useFormItemInit } from './hooks';
import { FormItemBaseProps } from './interface';
import React from 'react';
import { useDrag } from './drop.hooks';
import { CheckboxGroupProps } from 'antd/es/checkbox';
import { Components } from '../..';




interface IProps extends FormItemBaseProps {
}
export default function MyCheckbox(props: IProps) {
    const { formControl, className = '', style={} } = props;
    const {
        value, setValue,
        readonly,
        outerClassName,
        options,
        layout,
        outerStyle,
        labelStyle,
    } = useFormItemInit(formControl, className, style);


    const onCheckChange = (checkedValue: any[]) => {
        setValue(checkedValue);
        formControl.setValue(checkedValue);
    };

    const checkboxProps: CheckboxGroupProps = {
        value: value,
        className: 'checkbox',
        options,
        onChange: onCheckChange,
        disabled: readonly,
    };

    /** 拖拽排序 */
    const dropRef = React.useRef<HTMLDivElement>(null);
    useDrag({ dropRef, formControl });

    return (
        <div component-name='form-item' className={outerClassName} style={outerStyle} ref={dropRef}>
            <div className={`field ${layout}`}>
                <Components.forms.FormLabel formControl={formControl} style={labelStyle} />
                <div className='control'>
                    <div className="value">
                        {
                            <Checkbox.Group {...checkboxProps} />   
                        }
                    </div>
                </div>
            </div>

        </div>
    )
}
